package cn.iocoder.yudao.module.system.controller.admin.a_neo4j;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.neo4j.driver.v1.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

import static org.neo4j.driver.v1.Values.parameters;

@Tag(name = "管理后台 - neo4j")
@RestController
@RequestMapping("/system/neo4j")
@Validated
public class Neo4jController {


    public static void main(String[] args) {
        Driver driver = GraphDatabase.driver("bolt://192.168.8.184:7687", AuthTokens.basic("neo4j", "neo4j"));
        Session session = driver.session();
        session.run("CREATE (n:bookName {name:'书名'})");


        String entity = "中央军委";
        String feature = "1930年";
        String relationship = "成立日期";
        String sen = "从中央军委成立到1930年年底，中央军委所做的工作，就是使军委对红军的指挥权力逐步提高、扩大；发布了一系列关于红军指挥、统一红军编制、红军军政关系等指示。";
        String entityType = "org";

        session.run("CREATE (n:" + "书名(表名)" + " {code:$code,entity: $entity,sen:$sen,entityType:$entityType})",
                parameters("code", UUID.randomUUID().toString(), "entity", entity, "sen", sen, "entityType", entityType));
        session.run("CREATE (n:" + "书名(表名)" + " {code:$code,entity: $entity,sen:$sen,entityType:$entityType})",
                parameters("code", UUID.randomUUID().toString(), "entity", feature, "sen", sen, "entityType", entityType));

        session.run("MATCH (b:书名(表名)),(n:" + "书名(表名)" + "),(m:" + "书名(表名)" + ") WHERE b.name='书名' AND n.entity='" + entity + "' AND  m.entity='" + feature + "'" +
                " CREATE (b)-[:包含]->(n) CREATE (m)-[" + relationship + "]->(n)");

        session.close();
        driver.close();
    }

    @GetMapping("/TestNeo4j")
    @Operation(summary = "TestNeo4j")
    @PreAuthorize("@ss.hasPermission('system:article:query')")
    public void TestNeo4j() {
        Driver driver = GraphDatabase.driver("bolt://192.168.8.184:7687", AuthTokens.basic("neo4j", "neo4j"));
        Session session = driver.session();
        session.run("CREATE (n:bookName {name：'书名'})");


        String entity = "中央军委";
        String feature = "1930年";
        String relationship = "成立日期";
        String sen = "从中央军委成立到1930年年底，中央军委所做的工作，就是使军委对红军的指挥权力逐步提高、扩大；发布了一系列关于红军指挥、统一红军编制、红军军政关系等指示。";
        String entityType = "org";
        String featureType = "成立日期";

        session.run("CREATE (n:" + entityType + " {code:$code,entity: $entity,sen:$sen})",
                parameters("code", UUID.randomUUID().toString(), "entity", entity, "sen", sen));
        session.run("CREATE (n:" + entityType + " {code:$code,entity: $entity,sen:$sen})",
                parameters("code", UUID.randomUUID().toString(), "entity", feature, "sen", sen));

        session.run("MATCH (b:bookName),(n:" + entityType + "),(m:" + entityType + ") WHERE b.name='书名' AND n.entity='" + entity + "' AND  m.entity='" + feature + "'" +
                " CREATE (b)-[:包含]->(n) CREATE (m)-[:" + relationship + "]->(n)");

        session.close();
        driver.close();
    }


}
